﻿using Ev_MapCom.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TESTCOM;

namespace Ev_MapCom
{
    public partial class Form1 : Form
    {
        int bwidth = 340;

        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 
        /// </summary>
        int tabIndex = 0;

        List<Bitmap> imgList = new List<Bitmap>() { Resources.水业娇子_触摸屏地图界面_01, Resources.水业娇子_触摸屏地图界面_03, Resources.水业娇子_触摸屏地图界面_02, Resources.水业娇子_触摸屏地图界面_04, Resources.水业娇子_触摸屏地图界面_05, Resources.水业娇子_触摸屏地图界面_06, Resources.水业娇子_触摸屏地图界面_07, Resources.水业娇子_触摸屏地图界面_08, Resources.水业娇子_触摸屏地图界面_09, Resources.水业娇子_触摸屏地图界面_10, Resources.水业娇子_触摸屏地图界面_11, Resources.水业娇子_触摸屏地图界面_12, Resources.水业娇子_触摸屏地图界面_13, Resources.水业娇子_触摸屏地图界面_14, Resources.水业娇子_触摸屏地图界面_15 };


        int tdefault = 60;
        int timerCount = 60;

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Start();
        }

        private void Timer1_Tick(object sender, EventArgs e)
        {
            timerCount--;
            if (timerCount <= 0) {
                //回退到首页重新选择省市区厂房
                timerCount = 60;
                tabIndex = 3;
                this.BackgroundImage = imgList[tabIndex - 1];
                toolsCOM.SendCmd("A0000");
                this.jiaClick();
            }
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            timerCount = tdefault ;
            var slipWidth = 40;
            switch (tabIndex)
            {
                case 4:
                    //四川省
                    if (e.X > 770 && e.X < 770 + bwidth && e.Y > 790 && e.Y < 890) {
                        //达州公司
                        //调用串口
                        toolsCOM.SendCmd("B0202");
                        //toolsCOM.SendCmdStr("42 30 32 30 32");
                    }
                    break;
                case 5:
                    var sdx01 = 610;
                    var sdx = 400;
                    //山东省
                    if (e.X > sdx01 && e.X < sdx01 + bwidth && e.Y > 620 && e.Y < 720)
                    {
                        //博山公司
                        //调用串口
                        toolsCOM.SendCmd("B0404");
                        //toolsCOM.SendCmdStr("42 30 34 30 34");
                    }
                    else if (e.X > sdx01 + bwidth + slipWidth && e.X < sdx01 + bwidth + slipWidth + bwidth && e.Y > 620 && e.Y < 720)
                    {
                        //淄川公司
                        //调用串口
                        toolsCOM.SendCmd("B0505");
                    }
                    //else if (e.X > sdx && e.X < sdx + bwidth && e.Y > 675 && e.Y < 775)
                    //{
                    //    //赛诺公司
                    //    //调用串口
                    //    toolsCOM.SendCmd("B0808");
                    //}
                    else if (e.X > sdx && e.X < sdx + bwidth && e.Y > 760 && e.Y < 900)
                    {
                        //恒台公司
                        //调用串口
                        toolsCOM.SendCmd("B0303");
                    }
                    else if (e.X > sdx + bwidth + slipWidth && e.X < sdx + bwidth + bwidth + slipWidth && e.Y > 760 && e.Y < 900)
                    {
                        //莱芜公司
                        //调用串口
                        toolsCOM.SendCmd("B0606");
                    }
                    else if (e.X > sdx + bwidth + bwidth + slipWidth + slipWidth && e.X < sdx + bwidth + bwidth + bwidth + slipWidth + slipWidth && e.Y > 760 && e.Y < 900)
                    {
                        //滨州公司
                        //调用串口
                        toolsCOM.SendCmd("B0707");
                    }
                    break;
                case 6:
                    var hbx = 407;
                    bwidth = 330;
                    //湖北省
                    if (e.X > hbx && e.X < hbx + bwidth && e.Y > 760 && e.Y < 870)
                    {
                        //黄冈公司
                        //调用串口
                        toolsCOM.SendCmd("B0909");
                    }
                    else if (e.X > hbx + bwidth + slipWidth && e.X < hbx + bwidth * 2 + slipWidth && e.Y > 760 && e.Y < 870)
                    {
                        //丹江口公司
                        //调用串口
                        toolsCOM.SendCmd("B1010");
                    }
                    else if (e.X > hbx + bwidth * 2 + slipWidth * 2 && e.X < hbx + bwidth * 3 + slipWidth * 2 && e.Y > 760 && e.Y < 870)
                    {
                        //荆门公司
                        //调用串口
                        toolsCOM.SendCmd("B1111");
                    }
                    break;
                case 7:
                    int sx = 662;
                    slipWidth = 80;
                    //河南省
                    if (e.X > sx && e.X < (sx + bwidth) && e.Y > 811 && e.Y < 911)
                    {
                        //沁阳公司
                        //调用串口
                        toolsCOM.SendCmd("B1313");
                    }
                    else if (e.X > (sx + bwidth + slipWidth) && e.X < (sx + bwidth + slipWidth + bwidth) && e.Y > 811 && e.Y < 911)
                    {
                        //灵宝公司
                        //调用串口
                        toolsCOM.SendCmd("B1212");
                    }
                    break;
                case 8:
                    //湖南省
                    if (e.X > 716 && e.X < 716 + bwidth && e.Y > 763 && e.Y < 863)
                    {
                        //湖南分公司
                        //调用串口
                        toolsCOM.SendCmd("B1414");
                    }
                    break;
                case 9:
                    int sxx = 580;
                    slipWidth = 60;
                    //河北省
                    if (e.X > sxx && e.X < sxx + bwidth && e.Y > 615 && e.Y < 720)
                    {
                        //定州公司
                        //调用串口
                        toolsCOM.SendCmd("B1515");
                    }
                    else if (e.X > (sxx + bwidth + slipWidth) && e.X < (sxx + bwidth + bwidth + slipWidth) && e.Y > 618 && e.Y < 720)
                    {
                        //张家口公司
                        //调用串口
                        toolsCOM.SendCmd("B1616");
                    }
                    //else if (e.X > sxx && e.X < sxx + bwidth && e.Y > 761 && e.Y < 880)
                    //{
                    //    //保定公司
                    //    //调用串口
                    //    toolsCOM.SendCmd("B4444");
                    //}
                    else if (e.X > 770 && e.X < (770 + bwidth) && e.Y > 761 && e.Y < 880)
                    {
                        //唐山丰南
                        //调用串口
                        toolsCOM.SendCmd("B5555");
                    }
                    break;
                case 10:
                    //北京
                    if (e.X > 900 && e.X < (900 + bwidth) && e.Y > 747 && e.Y < 847)
                    {
                        //通州公司
                        //调用串口
                        toolsCOM.SendCmd("B1717");
                    }
                    break;
                case 11:
                    int tx = 404;
                    //天津
                    if (e.X > tx && e.X < tx + bwidth && e.Y > 761 && e.Y < 861)
                    {
                        //塘沽公司
                        //调用串口
                        toolsCOM.SendCmd("B1919");
                    }
                    else if (e.X > (tx + bwidth + slipWidth) && e.X < (tx + bwidth + slipWidth + bwidth) && e.Y > 761 && e.Y < 861)
                    {
                        //津南公司
                        //调用串口
                        toolsCOM.SendCmd("B1818");
                    }
                    else if (e.X > (tx + bwidth + slipWidth + bwidth + slipWidth) && e.X < (tx + bwidth + slipWidth + bwidth + slipWidth + bwidth) && e.Y > 761 && e.Y < 861)
                    {
                        //大港公司
                        //调用串口
                        toolsCOM.SendCmd("B2020");
                    }
                    break;
                case 12:
                    //江苏
                    if (e.X > 790 && e.X < 790 + bwidth && e.Y > 753 && e.Y < 863)
                    {
                        //亭湖公司
                        //调用串口
                        toolsCOM.SendCmd("B2121");
                    }
                    break;
                case 13:
                    //浙江
                    if (e.X > 790 && e.X < 790 + bwidth && e.Y > 753 && e.Y < 863)
                    {
                        //温岭公司
                        //调用串口
                        toolsCOM.SendCmd("B2222");
                    }
                    break;
                case 14:
                    //广东省
                    if (e.X > 630 && e.X < 630 + bwidth && e.Y > 773 && e.Y < 887)
                    {
                        //阳西公司
                        //调用串口
                        toolsCOM.SendCmd("B2323");
                    }
                    break;
                case 15:
                    //海南省
                    if (e.X > 849 && e.X < 849 + 500 && e.Y > 723 && e.Y < 850)
                    {
                        //海口水环境治理公司
                        //调用串口
                        toolsCOM.SendCmd("B2424");
                    }
                    break;
                default:
                    break;
            }
        }



        #region 注册点击事件


        private void panel_Down_Click(object sender, EventArgs e)
        {
            timerCount = tdefault;
            if (tabIndex < imgList.Count)
            {
                tabIndex = tabIndex + 1;
                var img = imgList[tabIndex - 1];
                this.BackgroundImage = img;
            }
        }

        private void panel_Up_Click(object sender, EventArgs e)
        {
            timerCount = tdefault;
            if (tabIndex > 3)
            {
                tabIndex = tabIndex - 1;
                var img = imgList[tabIndex - 1];
                this.BackgroundImage = img;

                if (tabIndex == 3)
                {
                    this.jiaClick();
                }
                else {
                    this.jianClick();
                }
            }
        }


        /// <summary>
        /// 添加全国点击事件
        /// </summary>
        public void jiaClick()
        {
            timerCount = tdefault;
            panel_beijing.Click += panel_beijing_Click;
            panel_四川.Click += panel_四川_Click;
            panel_山东.Click += panel_山东_Click;
            panel_hubei.Click += panel_hubei_Click;
            panel_henan.Click += panel_henan_Click;
            panel_hunan.Click += panel_hunan_Click;
            panel_hebei.Click += panel_hebei_Click;
            panel_tianjing.Click += panel_tianjing_Click;
            panel_jiangsu.Click += panel_jiangsu_Click;
            panel_zhejiang.Click += panel_zhejiang_Click;
            panel_guangdong.Click += panel_guangdong_Click;
            panel_hainan.Click += panel_hainan_Click;
        }

        /// <summary>
        /// 取消全国点击事件
        /// </summary>
        public void jianClick() {
            panel_beijing.Click -= panel_beijing_Click;
            panel_四川.Click -= panel_四川_Click;
            panel_山东.Click -= panel_山东_Click;
            panel_hubei.Click -= panel_hubei_Click;
            panel_henan.Click -= panel_henan_Click;
            panel_hunan.Click -= panel_hunan_Click;
            panel_hebei.Click -= panel_hebei_Click;
            panel_tianjing.Click -= panel_tianjing_Click;
            panel_jiangsu.Click -= panel_jiangsu_Click;
            panel_zhejiang.Click -= panel_zhejiang_Click;
            panel_guangdong.Click -= panel_guangdong_Click;
            panel_hainan.Click -= panel_hainan_Click;
        }


        private void panel_四川_Click(object sender, EventArgs e)
        {
            tabIndex = Convert.ToInt32(panel_四川.Tag);
            this.BackgroundImage = Resources.水业娇子_触摸屏地图界面_04;
            this.jianClick();
        }

        private void panel_山东_Click(object sender, EventArgs e)
        {
            tabIndex = Convert.ToInt32(panel_山东.Tag);
            this.BackgroundImage = Resources.水业娇子_触摸屏地图界面_05;
            this.jianClick();
        }

        /// <summary>
        /// 湖北
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panel_hubei_Click(object sender, EventArgs e)
        {
            tabIndex = Convert.ToInt32(panel_hubei.Tag);
            this.BackgroundImage = Resources.水业娇子_触摸屏地图界面_06;
            this.jianClick();
        }
        /// <summary>
        /// 河南
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panel_henan_Click(object sender, EventArgs e)
        {
            tabIndex = Convert.ToInt32(panel_henan.Tag);
            this.BackgroundImage = Resources.水业娇子_触摸屏地图界面_07;
            this.jianClick();
        }
        /// <summary>
        /// 湖南
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panel_hunan_Click(object sender, EventArgs e)
        {
            tabIndex = Convert.ToInt32(panel_hunan.Tag);
            this.BackgroundImage = Resources.水业娇子_触摸屏地图界面_08;
            this.jianClick();
        }
        /// <summary>
        /// 河北
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panel_hebei_Click(object sender, EventArgs e)
        {
            tabIndex = Convert.ToInt32(panel_hebei.Tag);
            this.BackgroundImage = Resources.水业娇子_触摸屏地图界面_09;
            this.jianClick();
        }

        /// <summary>
        /// 北京
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panel_beijing_Click(object sender, EventArgs e)
        {
            tabIndex = Convert.ToInt32(panel_beijing.Tag);
            this.BackgroundImage = Resources.水业娇子_触摸屏地图界面_10;
            this.jianClick();
        }

        /// <summary>
        /// 天津
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panel_tianjing_Click(object sender, EventArgs e)
        {
            tabIndex = Convert.ToInt32(panel_tianjing.Tag);
            this.BackgroundImage = Resources.水业娇子_触摸屏地图界面_11;
            this.jianClick();
        }

        /// <summary>
        /// 江苏
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panel_jiangsu_Click(object sender, EventArgs e)
        {
            tabIndex = Convert.ToInt32(panel_jiangsu.Tag);
            this.BackgroundImage = Resources.水业娇子_触摸屏地图界面_12;
            this.jianClick();
        }

        /// <summary>
        /// 浙江
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panel_zhejiang_Click(object sender, EventArgs e)
        {
            tabIndex = Convert.ToInt32(panel_zhejiang.Tag);
            this.BackgroundImage = Resources.水业娇子_触摸屏地图界面_13;
            this.jianClick();
        }

        /// <summary>
        /// 广东
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panel_guangdong_Click(object sender, EventArgs e)
        {
            tabIndex = Convert.ToInt32(panel_guangdong.Tag);
            this.BackgroundImage = Resources.水业娇子_触摸屏地图界面_14;
            this.jianClick();
        }

        /// <summary>
        /// 海南
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panel_hainan_Click(object sender, EventArgs e)
        {
            tabIndex = Convert.ToInt32(panel_hainan.Tag);
            this.BackgroundImage = Resources.水业娇子_触摸屏地图界面_15;
            this.jianClick();
        }


        #endregion

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            //toolsCOM.closeCmd();
        }

        /// <summary>
        /// 回到首页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panel_home_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 回退到选择项
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panel_back_Click(object sender, EventArgs e)
        {
            if (tabIndex == 3) {
                this.Close();
                return;
            }
            tabIndex = 3;
            this.BackgroundImage = imgList[tabIndex - 1];
            toolsCOM.SendCmd("A0000");
            this.jiaClick();
        }
    }
}
